package cn.atc.controller;

import cn.atc.pojo.*;
import cn.atc.service.AdminService;
import cn.atc.service.AssemblytaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/page")
public class AssemblytaskController {
    @Autowired
    private AssemblytaskService assemblytaskService;

    @Autowired
    private AdminService adminService;

    @RequestMapping("/assemblytask")
    public String toList(){
        return "assemblytask";
    }

    @RequestMapping("/assemblytask_list")
    @ResponseBody
    public Msg getAssemblytask(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        try {
            //在查询前调用，传入页码以及分页的大小
            PageHelper.startPage(pn, 2);
            //直接进行查询
            List<Assemblytask> assemblytasks= assemblytaskService.getAssemblytaskList();
            //查询后进行包装，并传入需要连续显示的页数
            PageInfo pageInfo=new PageInfo(assemblytasks,1);
            return Msg.success().add("pageInfo",pageInfo);
        }catch (Exception e){
            return Msg.fail();
        }

    }

    @RequestMapping(value = "/assemblytask_add",method = RequestMethod.POST)
    @ResponseBody
    public Msg save(Assemblytask assemblytask){
        try {
            //从ajax中拿到的date为字符串，需手动转换
            String dateStr=assemblytask.getDateStr();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            assemblytask.setEstimatedTimeOfCompletion(sdf.parse(dateStr));
            assemblytaskService.saveAssemblytask(assemblytask);
        }catch (Exception e){
            e.printStackTrace();
            return Msg.fail();
        }
        return Msg.success();
    }

    @RequestMapping(value = "/assemblytask_del/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg delete(@PathVariable("ids") String ids){
        try {
            if(ids.contains("-")){
                //批量删除
                String[] strings = ids.split("-");
                for(String id:strings){
                    assemblytaskService.deleteAssemblytask(id);
                }
            }
            else {
                //单个删除
                assemblytaskService.deleteAssemblytask(ids);
            }
            return Msg.success();
        }catch (Exception e){
            return Msg.fail();
        }

    }

    /**
     *  更新装配任务书
     */
    @RequestMapping(value = "/assemblytask_edit/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg update(Assemblytask assemblytask) {
        try {
            System.out.println(assemblytask);
            String dateStr=assemblytask.getDateStr();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            assemblytask.setEstimatedTimeOfCompletion(sdf.parse(dateStr));
            System.out.println(assemblytask);
            assemblytaskService.updateBySelective(assemblytask);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
        return Msg.success();
    }

    /**
     * 获取单个装配任务书信息，带其他实体信息
     */
    @RequestMapping(value = "/assebmlytask1/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Msg getOne(@PathVariable("id") String id) {
        try {
            Assemblytask assemblytask = assemblytaskService.selectByIdWithEntitys(id);
            assemblytask.setDateStr(
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .format(assemblytask.getEstimatedTimeOfCompletion()
                            )
            );
            return Msg.success().add("assemblytask", assemblytask);
        } catch (Exception e) {
            return Msg.fail();
        }
    }


    /**
     * TODO:这个请求应该在FinishedproductstypeController中，包括service也应该用它的，但是它没被实现，所以暂时写在这里
     * 获取成品类型
     * @return
     */
    @RequestMapping(value = "/finishedtypes")
    @ResponseBody
    public Msg getFinishedTypes(){
        List<Finishedproductstype> finishedTypesList = assemblytaskService.getFinishedTypesList();
        return Msg.success().add("finishedtypes",finishedTypesList);
    }

    @RequestMapping(value = "/assemblyLeaders")
    @ResponseBody
    public Msg getAssemblyLeaders(){
        List<Admin> admins = adminService.queryAll();
        return Msg.success().add("assemblyLeaders",admins);
    }

    /**
     * TODO:这个请求应该在PartformulaController中，包括service也应该用它的，但是它没被实现，所以暂时写在这里
     * 获取零件配方类型
     * @return
     */
    @RequestMapping(value = "/partformulas")
    @ResponseBody
    public Msg getPartformulas(){
        List<Partformula> partformulas = assemblytaskService.getParformulaList();
        return Msg.success().add("partformulas",partformulas);
    }

}
